package com.ruoyi.map.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.map.domain.BuildingInfo;
import com.ruoyi.map.domain.VillageInfor;
import com.ruoyi.map.mapper.BuildingInfoMapper;
import com.ruoyi.map.mapper.VillageInforMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.map.mapper.RegionInfoMapper;
import com.ruoyi.map.domain.RegionInfo;
import com.ruoyi.map.service.IRegionInfoService;

import javax.annotation.Resource;

/**
 * 保障房建筑区域信息数据Service业务层处理
 *
 * @author dihang
 * @date 2023-07-25
 */
@Service
public class RegionInfoServiceImpl extends ServiceImpl<RegionInfoMapper, RegionInfo> implements IRegionInfoService
{

    @Resource
    RegionInfoMapper regionInfoMapper;
    @Resource
    VillageInforMapper villageInforMapper;
    @Resource
    BuildingInfoMapper buildingInfoMapper;

    /**
     * 查询保障房建筑区域信息数据列表
     *
     *
     * @param
     * @return 保障房建筑区域信息数据列表
     */
    @Override
    public List<RegionInfo> selectRegionInfoList()
    {
        List<RegionInfo> regionInfos = regionInfoMapper.selectList(null);
        if (regionInfos.isEmpty()){
            throw new BaseException("保障房建筑区域信息数据为空！");
        }
        for (RegionInfo regionInfo : regionInfos) {
            //区域id
            Long id = regionInfo.getId();
            //查询出该区域所有的楼栋
            List<BuildingInfo> buildingInfoList=buildingInfoMapper
                    .selectList(Wrappers.<BuildingInfo>lambdaQuery().eq(BuildingInfo::getRegionId,id));
            //计算楼栋数量
            int size = buildingInfoList.size();
            //进行赋值
            regionInfo.setIncludeBuildingNum(size);
            //查询楼栋名称集合
            List<String> buildingName = buildingInfoList.stream().map(BuildingInfo::getName).collect(Collectors.toList());
            regionInfo.setIncludeBuildingName(buildingName);
        }
        return regionInfos;
    }

    /**
     * 新增保障房建筑区域信息数据列表
     * @param regionInfo
     */
    @Override
    public void addNew(RegionInfo regionInfo) {
        if (regionInfo.getId()==null){
            throw new BaseException("主键id不能为空！");
        }
        LambdaQueryWrapper<RegionInfo>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(RegionInfo::getName,regionInfo.getName());
        queryWrapper.eq(RegionInfo::getDeleted,"0");////deleted为是否删除 0 否 1 是
        Long selectCount = regionInfoMapper.selectCount(queryWrapper);
        if (selectCount>0){
            throw new BaseException("该区域已存在，不能重复录入！");
        }
        //传入参数时，根据填入的所属小区来确定小区的主键ID
        String affiliationVillage = regionInfo.getAffiliationVillage();
        LambdaQueryWrapper<VillageInfor>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(VillageInfor::getName,affiliationVillage);
        VillageInfor villageInfor = villageInforMapper.selectOne(lambdaQueryWrapper);
        if (villageInfor==null){
            throw new BaseException("数据加库中没有您填写得到所属小区！");
        }
        Long id = villageInfor.getId();
        //将小区的主键ID赋值给区域的所属小区ID
        regionInfo.setVillageId(id);
        //插入数据
        this.save(regionInfo);
    }


    /**
     * 分页-条件查询保障房建筑区域信息数据列表
     *
     * @param regionInfo
     * @return
     */
    @Override
    public IPage<RegionInfo> regionSearch(RegionInfo regionInfo) {
        //按条件分页查询
        IPage<RegionInfo>page=this.page(new Page<>(regionInfo.getPageNum(),
                        regionInfo.getPageSize()),oneSelectWhere(regionInfo));
        //转化为List集合
        List<RegionInfo> regionInfoList = page.getRecords();
        List<RegionInfo>list=new ArrayList<>();
        for (RegionInfo region : regionInfoList) {
            //区域id
            Long id = region.getId();
            //查询出该区域所有的楼栋
            List<BuildingInfo> buildingInfoList=buildingInfoMapper
                    .selectList(Wrappers.<BuildingInfo>lambdaQuery().eq(BuildingInfo::getRegionId,id));
            //计算楼栋数量
            int size = buildingInfoList.size();
            //进行赋值
            region.setIncludeBuildingNum(size);
            //查询楼栋名称集合
            List<String> buildingName = buildingInfoList.stream().map(BuildingInfo::getName).collect(Collectors.toList());
            region.setIncludeBuildingName(buildingName);
            list.add(region);
        }
        IPage<RegionInfo>iPage=new Page<>();
        iPage.setRecords(list);
        return iPage;
    }


    /**
     * 条件查询 封装QueryWrapper
     *
     * @param regionInfo
     */
    private LambdaQueryWrapper<RegionInfo>oneSelectWhere(RegionInfo regionInfo){
        LambdaQueryWrapper<RegionInfo>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (regionInfo==null){
            return lambdaQueryWrapper;
        }
        lambdaQueryWrapper.like(StrUtil.isNotBlank(regionInfo.getName()),
                RegionInfo::getName,regionInfo.getName())
                .eq(RegionInfo::getDeleted,"0");
        lambdaQueryWrapper.orderByDesc(RegionInfo::getCreateTime);
        return lambdaQueryWrapper;
    }

    /**
     * 获取保障房建筑区域信息数据详细信息
     * @param id
     * @return
     */
    @Override
    public RegionInfo selectById(Long id) {
        RegionInfo regionInfo = regionInfoMapper.selectById(id);
        Long id1 = regionInfo.getId();
        List<BuildingInfo> buildingInfos = buildingInfoMapper.selectList(Wrappers.<BuildingInfo>lambdaQuery().eq(BuildingInfo::getRegionId, id1));
        int size = buildingInfos.size();
        //计算区域包含的楼栋数量
        regionInfo.setIncludeBuildingNum(size);
        //将该区域所包含的所有楼栋整理成一个集合
        List<String >nameList=buildingInfos.stream().map(BuildingInfo::getName).collect(Collectors.toList());
        //进行赋值
        regionInfo.setIncludeBuildingName(nameList);
        return regionInfo;
    }

}
